package com.syezon.wifikey.speed;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.syezon.wifikey.utils.Utils;

import java.lang.reflect.Method;

public class DeviceInfoUtils {


//    TelephonyManager telmanager = (TelephonyManager) Utils.getApp().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
//    String st = telmanager.getSubscriberId();
//            if (st != null) {
//        if (st.startsWith("46000") || st.startsWith("46002"))//移到的46000字段占用完了，因此借用了460002字段因此这2个都是移动用户。
//        {
//            isp = "中国移动";
//        } else if (st.startsWith("46001")) {
//            isp = "中国联通";
//
//        } else if (st.startsWith("46003")) {
//            isp = "中国电信";
//        }
//    }

    public static String getBrandModel() {
        return Build.BRAND + "_" + Build.MODEL;
    }

    public static String getIsp() {
        String isp = "移动网络";
        try {
            int cellularOperatorType = getCellularOperatorType();
            if (cellularOperatorType == 1) {
                isp = "中国移动";
            }

            if (cellularOperatorType == 2) {
                isp = "中国联通";
            }

            if (cellularOperatorType == 3) {
                isp = "中国电信";
            }

        } catch (Exception e) {

        }
        return isp;
    }

    /**
     * 获取设备蜂窝网络运营商
     *
     * @return ["中国电信CTCC":3]["中国联通CUCC:2]["中国移动CMCC":1]["other":0]["无sim卡":-1]["数据流量未打开":-2]
     */
    public static int getCellularOperatorType() {
        int opeType = -1;
        // No sim
        if (!hasSim(Utils.getApp())) {
            return opeType;
        }
        // Mobile data disabled
        if (!isMobileDataEnabled(Utils.getApp())) {
            opeType = -2;
            return opeType;
        }
        // Check cellular operator
        TelephonyManager tm = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
        String operator = tm.getSimOperator();
        // 中国联通
        if ("46001".equals(operator) || "46006".equals(operator) || "46009".equals(operator)) {
            opeType = 2;
            // 中国移动
        } else if ("46000".equals(operator) || "46002".equals(operator) || "46004".equals(operator) || "46007".equals(operator)) {
            opeType = 1;
            // 中国电信
        } else if ("46003".equals(operator) || "46005".equals(operator) || "46011".equals(operator)) {
            opeType = 3;
        } else {
            opeType = 0;
        }
        return opeType;
    }

    /**
     * 判断数据流量开关是否打开
     *
     * @param context
     * @return
     */
    public static boolean isMobileDataEnabled(Context context) {
        try {
            Method method = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            return (Boolean) method.invoke(connectivityManager);
        } catch (Throwable t) {
            return false;
        }
    }

    /**
     * 检查手机是否有sim卡
     */
    private static boolean hasSim(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = tm.getSimOperator();
        if (TextUtils.isEmpty(operator)) {
            return false;
        }
        return true;
    }

}
